/*
 * Copyright 2019 bingoogolapple
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.bingoogolapple.transformerstip.demo;

import cn.bingoogolapple.transformerstip.SimpleTextTip;
import cn.bingoogolapple.transformerstip.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 列表点击后弹出对话框效果测试
 *
 * @author 王浩 邮件:bingoogolapple@gmail.com
 * @since 2021-04-25
 */
public class RecyclerViewTestActivity extends AbilitySlice {
    private static final int WIDTH_SIMPLE_TEXT_TIP = 700;

    private static final int HEIGHT_SIMPLE_TEXT_TIP = 200;

    private static final int DELAY_TIME = 800;

    private static final int WIDTH_ARROW = 50;

    private static final int HEIGHT_ARROW = 30;

    private static final int RADIUS = 20;

    private static final int VIEW_TWO = 2;

    private static final int FIVE_HUNDRED = 500;

    private static final float VIEW_ALPHA = 0.8F;

    private ListContainer mContentRv;

    private BaseItemProvider mAdapter;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_recyclerview_test);
        mContentRv = (ListContainer) findComponentById(ResourceTable.Id_rv_content);
        initRecyclerView();
    }

    private void initRecyclerView() {
        mAdapter = new BaseItemProvider() {
            @Override
            public int getCount() {
                return FIVE_HUNDRED;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
                Component parse = component;
                ViewHolder viewHolder;

                if (parse == null) {
                    parse = LayoutScatter.getInstance(getContext())
                            .parse(ResourceTable.Layout_activity_recyclerview_test_adapter, null, false);
                    viewHolder = new ViewHolder(parse);
                    parse.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) parse.getTag();
                }
                String title = (position % VIEW_TWO) == 0 ? "->点击浮层后 notify 当前条目" : "->点击浮层后向上滚动 8 个条目";
                viewHolder.textView.setText("第 " + position + " 项" + title);
                viewHolder.textView.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        showTip(viewHolder.textView, position);
                    }
                });
                return parse;
            }
        };
        mContentRv.setItemProvider(mAdapter);
        mContentRv.setTouchFocusable(true);
    }

    /**
     * 动态卡片容器
     *
     * @author 王浩 邮件:bingoogolapple@gmail.com
     * @since 2021-04-25
     */
    public static class ViewHolder {
        Text textView;

        ViewHolder(Component itemComponent) {
            textView = (Text) itemComponent.findComponentById(ResourceTable.Id_name);
        }
    }

    private void showTip(Text anchorView, final int position) {
        Component component = getComponentText(position);
        SimpleTextTip simpleTextTip = new SimpleTextTip(anchorView, component) {
            @Override
            public boolean isTouchOutside() {
                return true;
            }

            @Override
            protected void initView(Component contentView) {
                contentView.findComponentById(contentView.getId()).setTouchEventListener(
                        new Component.TouchEventListener() {
                            @Override
                            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                                switch (touchEvent.getAction()) {
                                    case TouchEvent.PRIMARY_POINT_DOWN:
                                        if ((position % VIEW_TWO) == 0) {
                                            anchorView.setAlpha(VIEW_ALPHA);
                                            mAdapter.notifyDataChanged();
                                        }
                                        break;
                                    case TouchEvent.PRIMARY_POINT_UP:
                                        if ((position % VIEW_TWO) == 0) {
                                            anchorView.setAlpha(1);
                                            mAdapter.notifyDataChanged();
                                        } else {
                                            int visibleIndexCount = mContentRv.getVisibleIndexCount() - VIEW_TWO;
                                            if (position > visibleIndexCount / VIEW_TWO) {
                                                mContentRv.scrollToCenter(position - visibleIndexCount / VIEW_TWO);
                                                dismissTip();
                                                getUITaskDispatcher().delayDispatch(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        showTip(anchorView, position);
                                                        mAdapter.notifyDataChanged();
                                                    }
                                                }, DELAY_TIME);
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                return true;
                            }
                        });
            }
        };
        setTip(simpleTextTip);
    }

    private Component getComponentText(int position) {
        Component component = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_simple_text, null, false);
        Text text = (Text) component.findComponentById(ResourceTable.Id_simple_text);
        text.setTextColor(Color.WHITE);
        text.setTextAlignment(TextAlignment.START);
        String title = (position % VIEW_TWO) == 0 ? "点击浮层后 notify 当前条目" : "点击浮层后向上滚动 8 个条目";
        text.setText("第 " + position + " 项的浮层" + System.lineSeparator() + title);
        return component;
    }

    private void setTip(SimpleTextTip simpleTextTip) {
        simpleTextTip.setSize(WIDTH_SIMPLE_TEXT_TIP, HEIGHT_SIMPLE_TEXT_TIP);
        simpleTextTip.setMode(LayoutAlignment.BOTTOM);
        simpleTextTip.setHasArrow(true);
        simpleTextTip.setAutoClosable(true);
        simpleTextTip.setTransparent(true);
        simpleTextTip.setCornerRadius(RADIUS);
        simpleTextTip.setArrowSize(WIDTH_ARROW, HEIGHT_ARROW);
        simpleTextTip.setBackColor(Color.BLACK);
        simpleTextTip.show();
    }
}
